package com.sinping.qh.api.admin.common;

import com.sinping.qh.api.support.Const;
import com.sinping.qh.api.support.FrontPage;
import com.sinping.qh.api.support.ResDto;
import com.sinping.qh.dto.admin.ApiBuildingDto;
import com.sinping.qh.service.admin.IBuildingService;
import com.sinping.qh.service.auth.ISessionService;
import com.sinping.qh.utils.mapper.JsonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;

/**
 * 楼宇
 *
 * @author Tiansx
 * @create 2018-05-29 11:04
 **/
@Api(value = "admin基础信息管理", description = "基础信息管理API")
@RequestMapping(value = Const.ADMIN_URI, name = "基础信息管理")
@RestController
public class BuildingEndPoint {
    @Autowired
    private Validator globalValidator;
    @Autowired
    private IBuildingService iBuildingService;
    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    private ISessionService sessionService;

    private final static Logger logger = LoggerFactory.getLogger(UnitEndPoint.class);

    /**
     * 新增楼宇
     *
     * @param msg
     * @return ResDto
     */
    @ApiOperation(value = "新增楼宇", notes = "楼宇名称 // 不超过30个字符，不可重复<br>")
    @RequestMapping(value = "building", method = RequestMethod.POST, name = "新增楼宇")
    public ResDto insert(HttpServletRequest request,@ApiParam(name = "msg", value = "{\"building_name\":\"光电学院科技大楼\"}") @RequestParam(required = true) String msg) {

        ApiBuildingDto apiBuildingDto = JsonMapper.defaultMapper().fromJson(msg, ApiBuildingDto.class);
        if (apiBuildingDto == null) {
            logger.error("参数为空或有不正确的数据格式");
            return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
        }
        Set<ConstraintViolation<ApiBuildingDto>> validate = globalValidator.validate(apiBuildingDto);
        if (!validate.isEmpty()) {
            for (ConstraintViolation<ApiBuildingDto> apiOrgSalesDTOConstraintViolation : validate) {
                logger.info("必传参数 :{}", apiOrgSalesDTOConstraintViolation.getMessage());
                return new ResDto(Const.PARAM_ERROR, apiOrgSalesDTOConstraintViolation.getMessage());
            }

        }

        return iBuildingService.addBuilding(request,apiBuildingDto);


    }

    /**
     * 修改楼宇信息
     *
     * @param msg
     * @return ResDto
     */
    @ApiOperation(value = "修改楼宇信息", notes = "楼宇名称 // 不超过30个字符，不可重复<br>")
    @RequestMapping(value = "building", method = RequestMethod.PUT, name = "修改楼宇信息")
    public ResDto update(HttpServletRequest request,@ApiParam(name = "msg", value = "{\"id\":\"1234\",\"building_name\":\"光电学院科技大楼\"}") @RequestParam(required = true) String msg) {
        ApiBuildingDto apiBuildingDto = JsonMapper.defaultMapper().fromJson(msg, ApiBuildingDto.class);
        if (apiBuildingDto == null) {
            logger.error("参数为空或有不正确的数据格式");
            return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
        }
        if (!StringUtils.isNotBlank(apiBuildingDto.getId())) {
            logger.info("必传参数 :{}", "id");
            return new ResDto(Const.PARAM_ERROR, "id");
        }
        Set<ConstraintViolation<ApiBuildingDto>> validate = globalValidator.validate(apiBuildingDto);
        if (!validate.isEmpty()) {

            for (ConstraintViolation<ApiBuildingDto> apiOrgSalesDTOConstraintViolation : validate) {
                logger.info("必传参数 :{}", apiOrgSalesDTOConstraintViolation.getMessage());
                return new ResDto(Const.PARAM_ERROR, apiOrgSalesDTOConstraintViolation.getMessage());
            }

        }


        return iBuildingService.updateBuilding(request,apiBuildingDto);


    }

    /**
     * 删除楼宇信息
     *
     * @param id
     * @return ResDto
     */
    @ApiOperation(value = "删除楼宇信息")
    @RequestMapping(value = "building/{id}", method = RequestMethod.DELETE, name = "删除楼宇信息")
    public ResDto delete(@ApiParam(name = "id", value = "245ad13d6ad4") @PathVariable String id) {

        if (!StringUtils.isNotBlank(id)) {
            logger.info("必传参数 :{}", "id");
            return new ResDto(Const.PARAM_ERROR, "id");
        }

        return iBuildingService.deleteBuilding(id);


    }

    /**
     * 获取楼宇详情
     * @param id
     * @return
     */
    @ApiOperation(value = "获取楼宇详情")
    @RequestMapping(value = "building/{id}", method = RequestMethod.GET, name = "获取楼宇详情")
    public ResDto getBuildingDetail(@ApiParam(name = "id", value = "245ad13d6ad4") @PathVariable String id) {

        if (!StringUtils.isNotBlank(id)) {
            logger.info("必传参数 :{}", "id");
            return new ResDto(Const.PARAM_ERROR, "id");
        }

        return iBuildingService.geteBuildingDetail(id);


    }
    /**
     * 楼宇列表,分页
     *
     * @param msg
     * @return ResDto
     */
    @ApiOperation(value = "楼宇列表,分页")
    @RequestMapping(value = "building_page", method = RequestMethod.GET, name = "楼宇列表，分页")
    public ResDto listPage(@ApiParam(name = "msg", value = "{\"page_size\":10, \"page_current\":1, \"order_column\":null, \"order_by\":null, \"condition\":{\"building_name\":\"大楼\"}}") @RequestParam(required = true) String msg) {

        try {
            logger.info("入参 {}", msg);
            //1.String msg=> FrontPage page
            FrontPage page = JsonMapper.defaultMapper().fromJson(msg, FrontPage.class);
            if (page == null) {
                logger.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            ResDto resDto = iBuildingService.getBuildingListPage(page);
            return resDto;
        } catch (Exception e) {
            logger.error("数据解析异常{}", e);
            ResDto resDto = new ResDto(Const.SERVER_ERROR,"数据解析异常");
            return resDto;
        }

    }
    /**
     * 楼宇列表,不分页
     * @return ResDto
     */
    @ApiOperation(value = "楼宇列表,不分页")
    @RequestMapping(value = "building", method = RequestMethod.GET, name = "楼宇列表，不分页")
    public ResDto list() {

        try {

            ResDto resDto = iBuildingService.getBuildingList();
            return resDto;
        } catch (Exception e) {
            logger.error("数据解析异常{}", e);
            ResDto resDto = new ResDto(Const.SERVER_ERROR,"数据解析异常");
            return resDto;
        }

    }
}
